<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript">
        var isIE = /msie/i.test(navigator.userAgent) && !window.opera;

        function fileChange(target) {
            var fileSize = 0;
            var fileTypes = [".jpg", ".png", ".rar", ".txt", ".zip", ".doc", ".ppt", ".xls", ".pdf", ".docx", ".xlsx"];
            var filePath = target.value;
            var fileMaxSize = 1024 * 100;//2M
            if (filePath) {
                var isNext = false;
                var fileEnd = filePath.substring(filePath.indexOf("."));
                if (fileTypes && fileTypes.length > 0) {
                    for (var i = 0; i < fileTypes.length; i++) {
                        if (fileTypes[i] == fileEnd) {
                            isNext = true;
                            break;
                        }
                    }
                }
                if (!isNext) {
                    alert("不接受此文件类型！");
                    target.value = "";
                    return false;
                }
            } else {
                return false;
            }
            if (isIE && !target.files) {
                var filePath = target.value;
                var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                if (!fileSystem.FileExists(filePath)) {
                    alert("附件不存在，请重新输入！");
                    return false;
                }
                var file = fileSystem.GetFile(filePath);
                fileSize = file.Size;
            } else {
                fileSize = target.files[0].size;
            }

            var size = fileSize / 1024;
            if (size > fileMaxSize) {
                alert("附件大小不能大于" + fileMaxSize / 1024 + "M！");
                target.value = "";
                return false;
            }
            if (size <= 0) {
                alert("附件大小不能为0M！");
                target.value = "";
                return false;
            }
        }
    </script>
</head>
<body>
<input type="file" name="contractFileName" style="width: 500px;" onchange="fileChange(this);"/>
</body>
</html> 
